<template>
	<view class="box">
		<view class="header" @click="chooseImg">
			<image :src="getUserInfo.avatarAddress ? getUserInfo.avatarAddress: '../../../static/img/theHead/grzxDefault.png'"></image>
			<view>点击修改头像</view>
		</view>
		
		<view class="mainContent">
			<view class="item">
				<text class="fixed">昵&emsp;称</text>
				<text class="divider"></text>
				<input type="text" placeholder="请输入昵称" v-model="nickName" @blur="nickNameBlur">
			</view>
			<view class="item">
				<text class="fixed">邮&emsp;箱</text>
				<text class="divider"></text>
				<input type="text" placeholder="请输入邮箱" v-model="email" @blur="emailBlur">
			</view>
			
			<view class="save" @click="save">保存</view>
		</view>
	</view>
</template>
<script>
	export default {
		data () {
			return {
				nickName: '',
				email: '',
				getUserInfo: {}
			}
		},
		onLoad() {
			if (!localStorage.token) {
				uni.navigateTo({
					url: '/pages/login/login'
				})
			}
			this.$api.getUserInfo().then((res) => {
				this.getUserInfo = res.data.result;
				this.nickName = this.getUserInfo.nickName;
				this.email = this.getUserInfo.email;
			})
		},
		methods: {
			chooseImg(){
				const _this = this;
				uni.chooseImage({
				    count: 1, //默认9
				    success: function (res) {
						var formdata = new FormData()
				       _this.$api.uploadImg(res.tempFilePaths[0],{type:12}).then((response) => {
						   if(response.returnCode == '200') {
							   _this.getUserInfo.avatarAddress = response.result.url;
						   }
						  
					   })
				    }
				});
			},
			nickNameBlur() {
				if(this.nickName == '') {
					uni.showToast({
						title: '昵称不能为空',
						icon: 'none'
					})
				}
			},
			emailBlur() {
				let reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/; 
				if(!(reg.test(this.email))) {
					uni.showToast({
						title: '请输入正确的邮箱',
						icon: 'none'
					})
					return
				}
			},
			
			save() {
				if(this.nickName == '') {
					uni.showToast({
						title: '昵称不能为空',
						icon: 'none'
					})
					return
				}
				const data = {
					id: this.getUserInfo.id ? this.getUserInfo.id : '',
					headPortrait : this.getUserInfo.avatarAddress ? this.getUserInfo.avatarAddress : '',
					email: this.email ? this.email : '',
					nickName : this.nickName ? this.nickName : '',
				}
				this.$api.upUserInfo(data).then((res) => {
					if(res.data.returnCode == '200') {
						uni.showToast({
							title:'修改成功'
						});						
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/index/index'
							})
						},2000)
					}
				})
			}
		}
	}
</script>
<style lang="scss" scoped>
	@import '@/static/css/theHead/editAccountInformation/editAccountInformation';
</style>
